package com.lvluo.jd;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.lvluo.jd.api.init.IInformer;
import com.lvluo.jd.api.init.IInitializers;
import com.lvluo.jd.api.init.IStrategy;
import com.lvluo.jd.api.order.TransactionFactory;
import com.lvluo.jd.api.stock.Fundamentals;
import com.lvluo.jd.api.stock.IInfoPacks;
import com.lvluo.jd.api.stock.IMetrics;
import com.lvluo.jd.api.stock.IStatisticsGroup;
import com.lvluo.jd.api.stock.IStatisticsHistory;
import com.lvluo.jd.api.stock.Instrument;
import com.lvluo.jd.api.stock.SectorCode;

//您不需要自己import, 我们已经帮您import了所有您可能需要的类库。
// 策略类名：First1，您可以随意命名策略类名
public class RapidStage implements IStrategy {

	boolean fired = false;

	// 定义全局股票池
	private List<String> stockList = new ArrayList<>();

	private String[] industrys = new String[] { "A01", "A02", "A03", "A04",
			"A05", "B06", "B07", "B08", "B09", "B09", "B11", "B12", "C13",
			"C14", "C15", "C16", "C17", "C18", "C19", "C20", "C21", "C22",
			"C23", "C24", "C25", "C26", "C27", "C28", "C29", "C30", "C31",
			"C32", "C33", "C34", "C35", "C36", "C37", "C38", "C39", "C40",
			"C41", "C42", "C43", "D44", "D45", "D46", "E47", "E48", "E49",
			"E50", "F51", "F52", "G53", "G54", "G55", "G56", "G57", "G58",
			"G59", "H61", "H62", "I63", "I64", "I65", "J66", "J67", "J68",
			"J69", "K70", "L71", "L72", "M73", "M74", "M75", "M76", "N77",
			"N78", "N79", "N80", "N81", "P82", "Q83", "Q84", "R85", "R86",
			"R87", "R88", "R89", "S90" };

	private SectorCode[] sectorCode = new SectorCode[] { SectorCode.Energy,
			SectorCode.Materials, SectorCode.ConsumerDiscretionary,
			SectorCode.ConsumerStaples, SectorCode.Industrials,
			SectorCode.HealthCare, SectorCode.Financials,
			SectorCode.InformationTechnology,
			SectorCode.TelecommunicationServices, SectorCode.Utilities };

	private final int DAY = 30;

	// init: 策略入口, 回测启动时系统会调用这个接口并运行您的策略。
	@Override
	public void init(IInformer informer, IInitializers initializers) {

		// 股票标识符: 股票代码.交易所代码, 如“000001.SZ”;

		// informer: 输入日志, 您可以通过它的四种方法(info/warn/error/debug)
		informer.info("选择股票：新股");

		initializers.commission().percent(0.04);

		initializers.instruments((universe, fundamentalData) -> {

			initializers.events().statistics(
					(IStatisticsGroup stats, IInfoPacks info,
							TransactionFactory trans) -> {

						int txDate = formatDate(stats.getDate());
						Instrument[] instruments = initializers
								.industry(industrys);
						for (Instrument instrument : instruments) {

							if (DAY + instrument.getListedDate() >= txDate
									&& instrument.getListedDate() < txDate) {
								informer.info(instrument.getOrderBookId() + ":"
										+ instrument.getListedDate() + "---"
										+ txDate);
								stockList.add(instrument.getOrderBookId());
							}
						}
					});
		});

		initializers.events().beforeTrading(
				(universe, info, fundamentalData) -> {

					for (String stockCode : stockList) {

					}

				});

		// 这是第二步：注册事件。
		// 事件处理器: 该事件是您的策略核心代码, 事件在回测中会被不断触发, 在日交易回测模式中, 处理器在每一个交易日都会被触发一次;
		// @stats - IStatisticsGroup, stats包含了当前行情事件的各种属性，例如收盘价、最高价、最低价、成交量等。
		// @info - IInfoPack, info则包含了一些您当前持仓的状态信息;
		// @trans - ITransactionFactory, trans用来生成买卖委托
		initializers.events().statistics((stats, info, trans) -> {
			// 如果还没有买入股票，则开始买入总钱数的100%的平安银行并且保持持有

				if (!fired) {

					// 记得调用commit进行确认落单
					for (String stockCode : stockList) {
						informer.info("购买10%的" + stockCode + "股票");
						trans.buy(stockCode).percent(10).commit();
					}
					// 设置为已经买入
					fired = true;
				}
			});
		// <<< 事件处理器结尾
	}

	/**
	 * 判斷是否聯繫漲停
	 * 
	 * @param history
	 * @return
	 */
	boolean harden(IStatisticsHistory history) {
		int length = history.getOpeningPrice().length;
		for (int i = 0; i < length; i++) {
			if (history.getClosingPrice()[i] - history.getHighPrice()[i] >= 0) {
				double minus = history.getHighPrice()[i]
						- history.getLowPrice()[i];
				double percent = Math.round(minus * 100
						/ history.getLowPrice()[i]) - 10;
				if (percent > 0 || minus <= 0)
					return true;
				else {
					return false;
				}

			}

		}
		return false;
	}

	boolean LimitDown(IStatisticsHistory history) {
		int length = history.getOpeningPrice().length;
		for (int i = 0; i < length; i++) {
			if (history.getClosingPrice()[i] - history.getHighPrice()[i] >= 0) {
				double minus = history.getHighPrice()[i]
						- history.getLowPrice()[i];
				double percent = Math.round(minus * 100
						/ history.getLowPrice()[i]) - 10;
				if (percent > 0 || minus <= 0)
					return true;
			}

		}
		return false;
	}

	int formatDate(Date today) {
		return Integer.parseInt((new java.text.SimpleDateFormat("yyyyMMdd")
				.format(today)));
	}

}
